package com.agileboot.attendance.controller;

import com.agileboot.attendance.domain.project.ProjectApplicationService;
import com.agileboot.attendance.domain.project.dto.ProjectDTO;
import com.agileboot.attendance.domain.project.dto.ProjectOptionDTO;
import com.agileboot.attendance.domain.project.dto.ProjectQuery;
import com.agileboot.attendance.domain.project.dto.ProjectRequest;
import com.agileboot.common.core.base.BaseController;
import com.agileboot.common.core.dto.ResponseDTO;
import com.agileboot.common.core.page.PageDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 项目管理控制器
 * 
 * @author ruoyi
 */
@Api(tags = "项目管理")
@RestController
@RequestMapping("/attendance/project")
@RequiredArgsConstructor
public class ProjectController extends BaseController {

    private final ProjectApplicationService projectApplicationService;

    /**
     * 获取项目列表
     */
    @ApiOperation("查询项目列表")
    @PreAuthorize("@permission.has('attendance:project:list')")
    @GetMapping("/list")
    public ResponseDTO<PageDTO<ProjectDTO>> list(ProjectQuery query) {
        PageDTO<ProjectDTO> pageDTO = projectApplicationService.getProjectList(query);
        return ResponseDTO.ok(pageDTO);
    }

    /**
     * 获取项目详细信息
     */
    @ApiOperation("获取项目详细信息")
    @PreAuthorize("@permission.has('attendance:project:query')")
    @GetMapping("/{projectId}")
    public ResponseDTO<ProjectDTO> getInfo(@PathVariable Long projectId) {
        ProjectDTO project = projectApplicationService.getProjectDetail(projectId);
        return ResponseDTO.ok(project);
    }

    /**
     * 新增项目
     */
    @ApiOperation("新增项目")
    @PreAuthorize("@permission.has('attendance:project:add')")
    @PostMapping
    public ResponseDTO<Void> add(@Validated @RequestBody ProjectRequest request) {
        projectApplicationService.addProject(request);
        return ResponseDTO.ok();
    }

    /**
     * 修改项目
     */
    @ApiOperation("修改项目")
    @PreAuthorize("@permission.has('attendance:project:edit')")
    @PutMapping
    public ResponseDTO<Void> edit(@Validated @RequestBody ProjectRequest request) {
        projectApplicationService.updateProject(request);
        return ResponseDTO.ok();
    }

    /**
     * 删除项目
     */
    @ApiOperation("删除项目")
    @PreAuthorize("@permission.has('attendance:project:remove')")
    @DeleteMapping("/{projectId}")
    public ResponseDTO<Void> remove(@PathVariable Long projectId) {
        projectApplicationService.deleteProject(projectId);
        return ResponseDTO.ok();
    }

    /**
     * 导出项目
     */
    @ApiOperation("导出项目列表")
    @PreAuthorize("@permission.has('attendance:project:export')")
    @GetMapping("/export")
    public void export(ProjectQuery query, HttpServletResponse response) {
        projectApplicationService.exportProject(query, response);
    }

    /**
     * 导入项目
     */
    @ApiOperation("导入项目")
    @PreAuthorize("@permission.has('attendance:project:import')")
    @PostMapping("/import")
    public ResponseDTO<String> importData(MultipartFile file) {
        String message = projectApplicationService.importProject(file);
        return ResponseDTO.ok(message);
    }

    /**
     * 下载导入模板
     */
    @ApiOperation("下载导入模板")
    @PreAuthorize("@permission.has('attendance:project:import')")
    @GetMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        projectApplicationService.downloadImportTemplate(response);
    }

    /**
     * 获取项目选项列表
     */
    @ApiOperation("获取项目选项列表")
    @GetMapping("/options")
    public ResponseDTO<List<ProjectOptionDTO>> getOptions() {
        List<ProjectOptionDTO> projectOptions = projectApplicationService.getProjectOptions();
        return ResponseDTO.ok(projectOptions);
    }
} 